<?php

/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/31 0031
 * Time: 16:08
 */

namespace app\api\controller;


use app\common\service\ImgComService;
use think\Db;
use think\Request;

class Upload // extends ApiController// extends UserBase
{
    protected $request;
    /**
     * 文件上传
     */
    public function index()
    {
        //图片上传这里必须加

        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            header("Access-Control-Allow-Origin: *");
            exit;
        }
        $this->request = Request::instance();

        $file = $this->request->file('file');

        $path = ROOT_PATH . 'public/tmp/uploads/';


        $info = $file->move($path);

        if (!$info) {
            return apiError($file->getError());
        }

        $path = $path . $info->getSaveName();
        $data = $this->request->root() . '/tmp/uploads/' . $info->getSaveName();
        //        $insert = [
        //            'cate'     => 3,
        //            'name'     => $data,
        //            'original' => $info->getInfo('name'),
        //            'domain'   => '',
        //            'type'     => $info->getInfo('type'),
        //            'size'     => $info->getInfo('size'),
        //            'mtime'    => time(),
        //        ];
        //        $pid = Db::table('sms_file')->insertGetId($insert);
        //$insert['id'] = $pid;
        $imgCom = new ImgComService($path, 1);
        $imgCom->compressImg($path);

        $data = str_replace('\\', '/', $data);

        return apiSuccess($data);
    }
}
